package com.automannn.atm_back.modules.codeGenerator.service.impl;

import com.automannn.atm_back.modules.codeGenerator.entity.ColumnConfigEntity;
import com.automannn.atm_back.modules.codeGenerator.mapper.ColumnConfigEntityMapper;
import com.automannn.atm_back.modules.codeGenerator.service.ColumnConfigEntityService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (GeneratorColumnConfig)表服务实现类
 *
 * @author chenkaihai
 * @since 2020-07-12 16:44:05
 */
@Service
public class ColumnConfigEntityServiceImpl implements ColumnConfigEntityService {
    @Resource
    private ColumnConfigEntityMapper columnConfigEntityMapper;


    /**
     * 查询一个对象
     *
     * @param id return 实例对象
     */
    @Override
    public ColumnConfigEntity queryOne(String id) {
        return this.columnConfigEntityMapper.queryById(id);
    }

    /**
     * 查询所有对象
     * <p>
     * return 实例对象
     */
    @Override
    public IPage<ColumnConfigEntity> queryAll(Page<ColumnConfigEntity> page, ColumnConfigEntity generatorColumnConfig) {
        return this.columnConfigEntityMapper.queryAll(page, generatorColumnConfig);
    }

    /**
     * 新增数据
     *
     * @param generatorColumnConfig 实例对象
     * @return 实例对象
     */
    @Override
    public ColumnConfigEntity insert(ColumnConfigEntity generatorColumnConfig) {
        this.columnConfigEntityMapper.insert(generatorColumnConfig);
        return generatorColumnConfig;
    }

    /**
     * 修改数据
     *
     * @param generatorColumnConfig 实例对象
     * @return 实例对象
     */
    @Override
    public void update(ColumnConfigEntity generatorColumnConfig) {
        columnConfigEntityMapper.update(generatorColumnConfig);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.columnConfigEntityMapper.deleteById(id) > 0;
    }

    @Override
    public boolean deleteBatchByIds(String ids) {
        return columnConfigEntityMapper.deleteBatchIds(Arrays.asList(ids.split(","))) > 0;
    }

    /**
     * 新增数据
     *
     * @param generatorColumnConfigList 实体对象
     * @return 新增结果
     */
    @Override
    public void saveOrUpdate(List<ColumnConfigEntity> generatorColumnConfigList) {
        List<ColumnConfigEntity>   toUpdateList=
                generatorColumnConfigList.stream().filter(columnConfigEntity -> columnConfigEntity.getId() != null && columnConfigEntity.getId() != "").collect(Collectors.toList());
        List<ColumnConfigEntity> toAddList =
                generatorColumnConfigList.stream().filter(columnConfigEntity -> columnConfigEntity.getId() == null || columnConfigEntity.getId() == "").collect(Collectors.toList());
        toAddList.forEach(columnConfigEntity -> columnConfigEntityMapper.insert(columnConfigEntity));
        toUpdateList.forEach(
                columnConfigEntity -> columnConfigEntityMapper.updateById(columnConfigEntity));
    }
}
